package com.blb.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.blb.entity.Role;
import com.blb.entity.RoleExample;
import com.blb.entity.User;
import com.blb.mapper.RoleMapper;
import com.blb.service.RoleService;
import com.blb.utils.GetTimeUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.MybatisUtils;
import com.blb.utils.UUIDUtils;
import com.mysql.jdbc.StringUtils;


/**
 * 
 * 角色层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("role")
public class RoleController {
	private RoleService roleService=new RoleService();
	/**
	 * 
	 * 角色分页
	 * @param page
	 * @param limit
	 * @param username
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public LayuiPage PageData(int page, int limit, String rolename) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper= session.getMapper(RoleMapper.class);
		List<Role> list = roleMapper.page((page - 1) * limit, limit, rolename);

		RoleExample RoleExample = new RoleExample();
		if (!StringUtils.isNullOrEmpty(rolename)) {
			com.blb.entity.RoleExample.Criteria criteria = RoleExample.createCriteria();
			criteria.andRolenameLike(rolename);
		}
		int count = (int) roleMapper.countByExample(RoleExample);
		LayuiPage layuiPage = new LayuiPage();
		layuiPage.setData(list);
		layuiPage.setCount(count);
		return layuiPage;
	}
	/**
	 * 角色添加
	 * 
	 */
	@ResponseBody
	@RequestMapping("add")
	private void add(Role role,HttpSession httpSession) {
		try {
			role.setId(UUIDUtils.getID());
			role.setCreationUser(((User) httpSession.getAttribute("user")).getUsername());
			role.setCreationTime(GetTimeUtils.getTime());
			roleService.add(role);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 获取父级菜单
	 * @throws IOException 
	 */
	@RequestMapping("addshow")
	public String addshow(Model model) throws IOException{
		List<Map<String,Object>> list = roleService.getParentMenu();
		String menus="";
		if(!list.isEmpty()){
			menus = JSON.toJSON(list).toString();
		}
		model.addAttribute("menus", menus);
		return "role/role_add";
	}

		
	
	/**
	 * 查询角色名是否存在
	 * @return 
	 * 
	 */
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String rolename){
		System.out.println(rolename);
		try {
			Role role=roleService.selectByrolename(rolename);
			if (role != null) {
				return "用户名已存在";
			} else {
				return "";
			}
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
	/**
	 * 修改用户
	 * @return 
	 * @throws IOException 
	 * 
	 */
	@ResponseBody
	@RequestMapping("update")
	private String updateIsExist(Role role,HttpSession httpSession) throws IOException {
		Role roleed = roleService.getRoleByRoleid(role.getId());
		try {
			role.setCreationUser(roleed.getCreationUser());
			role.setCreationTime(roleed.getCreationTime());
			role.setUpdateTime(GetTimeUtils.getTime());
			role.setUpdateUser(((User) httpSession.getAttribute("user")).getUsername());
			roleService.updateRole(role);
		} catch (IOException e) {
			e.printStackTrace();
			return "修改失败" + e.getMessage();
		}
		return "";
	}
	/**
	 * 用户删除
	 * 	
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody List<String> ids) {
		try {
			roleService.delete(ids);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

}
	
	



